package com.findbus.view;

import com.findbus.project.R;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v4.widget.DrawerLayout.LayoutParams;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.TextView;

public class LoginDialog extends Dialog {

	private LoginDialog(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public LoginDialog(Context context, int theme) {
		super(context, theme);
		// TODO Auto-generated constructor stub
	}

	public static class Builder {
		private Context context;
		private String message;
		private String positiveButtonText;
		private DialogInterface.OnClickListener positiveButtonClickListener;
		private String negativeButtonText;
		private OnClickListener negativeButtonClickListener;
		private View layout;

		public Builder(Context context) {
			this.context = context;
		}

		public Builder setMessage(String message) {
			this.message = message;
			return this;
		}

		public Builder setMessage(int message) {
			this.message = (String) context.getText(message);
			return this;
		}

		public Builder setPositiveButton(int positiveButtonText, DialogInterface.OnClickListener listener) {
			this.positiveButtonText = (String) context.getText(positiveButtonText);
			this.positiveButtonClickListener = listener;
			return this;
		}

		public Builder setPositiveButton(String positiveButtonText, DialogInterface.OnClickListener listener) {
			this.positiveButtonText = positiveButtonText;
			this.positiveButtonClickListener = listener;
			return this;
		}

		public Builder setNegativeButton(int negativeButtonText, DialogInterface.OnClickListener listener) {
			this.negativeButtonText = (String) context.getText(negativeButtonText);
			this.negativeButtonClickListener = listener;
			return this;
		}

		public Builder setNegativeButton(String negativeButtonText, DialogInterface.OnClickListener listener) {
			this.negativeButtonText = negativeButtonText;
			this.negativeButtonClickListener = listener;
			return this;
		}

		@SuppressLint("InflateParams")
		public LoginDialog build() {
			layout = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
					.inflate(R.layout.dislog_login, null);
			final LoginDialog dialog = new LoginDialog(context, R.style.Theme_Transparent);
			dialog.addContentView(layout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

			Button p = (Button) layout.findViewById(R.id.dia_login_positiveButton);
			Button n = (Button) layout.findViewById(R.id.dia_login_negativeButton);
			if (positiveButtonText != null) 
				p.setText(positiveButtonText);
				p.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						if (positiveButtonClickListener != null)
							positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
						dialog.dismiss();
					}

				});
	

			if (negativeButtonText != null) 
				n.setText(negativeButtonText);

				n.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						if (negativeButtonClickListener != null)
							negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
						dialog.dismiss();
					}

				});
			
			if (message != null)
				((TextView) layout.findViewById(R.id.dia_login_title)).setText(message);
			return dialog;
		}

		@SuppressLint("RtlHardcoded")
		@SuppressWarnings("deprecation")
		public void show() {
			LoginDialog dia = build();
			WindowManager m =(WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
			Display d = m.getDefaultDisplay(); // 为获取屏幕宽、高
			android.view.WindowManager.LayoutParams p = dia.getWindow().getAttributes(); // 获取对话框当前的参值
			p.width = (int) (d.getWidth() * 0.9); // 宽度设置为屏幕的0.8
			p.alpha = 1.0f; // 设置本身透明度
			p.dimAmount = 0.2f; // 设置黑暗度
			dia.getWindow().setAttributes(p); // 设置生效
			dia.getWindow().setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL);
			TranslateAnimation animation = new TranslateAnimation(0, 0, -layout.getWidth(), 0);
			animation.setDuration(1000);
			layout.startAnimation(animation);
			dia.show();
		}
	}
}
